<template>
  <div class="banner-box w-card">
    <div class="title-box">
      <span class="title">banner管理</span>
      <el-button style="margin-left:auto;" type="primary" icon="Plus" @click="addBanner(false)">新增Banner</el-button>
    </div>
    <!-- <div class="search-box">
      <div>
        <span>类别：</span>
        <el-select v-model="searchForm.type" placeholder="请选择类别" style="width: 240px">
          <el-option v-for="item in bannerType" :key="item.value" :label="item.label" :value="item.value" />
        </el-select>
      </div>
      <el-button type="primary" @click="searchHandle">查询</el-button>
    </div> -->
    <div class="table-box">
      <el-table :height="tableHeight" :header-cell-style="{background:'#d6dee5',color:'#000000',fontSize:'14px'}"
        :cell-style="{fontSize:'14px',color:'#7b7b7d'}" :data="tableData" stripe>
        <el-table-column type="index" width="55" label="序号" align="center" />
        <el-table-column prop="headImg" label="Banner图片" align="center">
          <template v-slot="scope">
            <img style="max-width: 120px;" :src="scope.row.headImg" alt="">
          </template>
        </el-table-column>
        <!-- <el-table-column prop="type" label="类别" align="center">
          <template v-slot="scope">
            {{ getLabel(scope.row?.type) }}
          </template>
        </el-table-column> -->
        <!-- <el-table-column prop="linkUrl" label="跳转地址" align="center">
          <template v-slot="scope">
            {{ scope.row.linkUrl || '' }}
          </template>
        </el-table-column> -->
        <el-table-column prop="power" label="权重" align="center">
          <template v-slot="scope">
            {{ scope.row.power || '' }}
          </template>
        </el-table-column>
        <el-table-column prop="createTime" label="创建时间" align="center">
          <template v-slot="scope">
            {{ scope.row.createTime ? getDateTime(scope.row.createTime)[6] : '' }}
          </template>
        </el-table-column>
        <el-table-column prop="modifyTime" label="修改时间" align="center">
          <template v-slot="scope">
            {{ scope.row.modifyTime ? getDateTime(scope.row.modifyTime)[6] : '' }}
          </template>
        </el-table-column>
        <!-- <el-table-column prop="modifyUser" label="修改人" align="center" width="180">
          <template v-slot="scope">
            {{ scope.row.modifyUser || '' }}
          </template>
        </el-table-column> -->
        <el-table-column prop="Operations" label="操作" align="center" width="160" fixed="right">
          <template v-slot="scope">
            <div class="table-handle-box">
              <el-link style="font-size:14px" type="primary" @click="addBanner(true,scope.row)">编辑</el-link>
              <el-link style="font-size:14px;margin-left: 10px;" type="danger" @click="deleteHandle(scope.row.id)">删除</el-link>
            </div>
          </template>
        </el-table-column>
      </el-table>
      <div class="table-footer">
        <el-pagination style="margin-left:auto;" v-model:currentPage="page" v-model:page-size="size"
          :page-sizes="[10, 20, 50, 100]" small :background="true" layout="total, sizes, prev, pager, next, jumper" :total="total"
          @size-change="handleSizeChange" @current-change="handleCurrentChange" />
      </div>
    </div>
  </div>
  <edit-dialog ref="editDialogRef" @dialogClose="dialogClose" />
  <confirm-pass ref="confirmPassRef" @verification="verification" />
</template>

<script>
import { computed, onMounted, reactive, ref, toRefs } from 'vue'
import { getWindowWidthAndHeight, warningDeal, getDateTime, successDeal } from '@/utils/utils'
import api from './api'
import editDialog from './components/editDialog.vue'
import confirmPass from '@/components/confirmPass.vue'
import { useStateStore } from '@/store'
export default {
  components: {
    editDialog,
    confirmPass,
  },
  setup() {
    const editDialogRef = ref(null)
    const confirmPassRef = ref(null)
    const store = useStateStore()

    const state = reactive({
      deleteId: null,

      searchForm: {},

      tableHeight: 0,
      page: 1,
      size: 10,
      total: 0,
      tableData: [],

      bannerType: computed(() => {
        return store.getBannerType
      })
    })

    function getTableData() {
      const obj = {
        page: state.page,
        size: state.size,
        ...state.searchForm,
        orderBy: 'power desc'
      }
      api.bannerList(obj).then(res => {
        if (res.code == '200') {
          state.tableData = res.data.list
          state.total = res.data.total
        } else {
          warningDeal(res.msg)
        }
      }).catch(err => {
        console.log(err)
      })
    }

    function handleCurrentChange(e) {
      state.page = e
      getTableData()
    }

    function handleSizeChange(e) {
      state.size = e
      getTableData()
    }

    function searchHandle() {
      state.page = 1
      getTableData()
    }

    function dialogClose() {
      getTableData()
    }

    function addBanner(isEdit = false, data = {}) {
      editDialogRef.value.dialogShow(isEdit, data)
    }

    function deleteHandle(id) {
      state.deleteId = id
      const title = '删除提示'
      const msg = '确认删除该Banner图吗？'
      confirmPassRef.value.dialogShow(title, msg, false)
    }

    function submitDelete(id) {
      const obj = {
        id
      }
      api.bannerDelete(obj).then(res => {
        if (res.code == '200') {
          state.deleteId = null
          successDeal(res.msg)
          getTableData()
        } else {
          warningDeal(res.msg)
        }
      }).catch(err => {
        console.log(err)
      })
    }

    function verification() {
      submitDelete(state.deleteId)
    }

    function getLabel(value) {
      let label = ''
      state.bannerType && state.bannerType.map(item => {
        if (item.value == value) {
          label = item.label
        }
      })
      return label
    }

    onMounted(() => {
      let winHeight = getWindowWidthAndHeight()
      state.tableHeight = winHeight.windowHeight - 270
      getTableData()
    })

    return {
      ...toRefs(state),
      editDialogRef,
      confirmPassRef,
      handleCurrentChange,
      handleSizeChange,
      searchHandle,
      getDateTime,
      dialogClose,
      addBanner,
      deleteHandle,
      verification,
      getLabel,
    }
  }
}
</script>

<style lang="scss" scoped>
.banner-box {
  width: 100%;
  height: 100%;
  box-sizing: border-box;
  padding: 20px;
  .title-box {
    display: flex;
    align-items: center;
    > .title {
      font-size: 16px;
      font-weight: bold;
    }
    margin-bottom: 10px;
  }
  .search-box {
    box-sizing: border-box;
    padding: 20px;
    font-size: 14px;
    display: flex;
    align-items: center;
    gap: 10px;
  }
  .table-box {
    .table-footer {
      margin-top: 10px;
      display: flex;
      align-items: center;
    }
  }
}
</style>